package com.ruoyi.business.service.koc.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.koc.TbKocLevel;
import com.ruoyi.business.domain.koc.TbKocTaskType;
import com.ruoyi.business.mapper.koc.TbKocLevelMapper;
import com.ruoyi.business.mapper.koc.TbKocTaskTypeMapper;
import com.ruoyi.business.service.koc.ITbKocLevelService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * koc 等级服务层实现
 *
 * @author chenran
 */
@RequiredArgsConstructor
@Service
public class TbKocLeveServiceImpl extends ServiceImpl<TbKocLevelMapper, TbKocLevel> implements ITbKocLevelService {

    @Resource
    private TbKocTaskTypeMapper tbKocTaskTypeMapper;


    @Override
    public List<TbKocLevel> queryAll() {
        List<TbKocLevel> tbKocLevels = this.baseMapper.selectList();
        // 必做任务类型
        tbKocLevels.forEach(this::taskSupplement);
        return tbKocLevels;
    }

    @Override
    public TbKocLevel queryById(Long id) {
        TbKocLevel tbKocLevel = this.getById(id);
        return taskSupplement(tbKocLevel);
    }


    private TbKocLevel taskSupplement(TbKocLevel tbKocLevel) {
        String mustKocTaskType = tbKocLevel.getMustKocTaskType();
        if (StringUtils.isNotEmpty(mustKocTaskType)) {
            List<String> mustKocTaskTypes = Arrays.stream(mustKocTaskType.split(","))
                .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(mustKocTaskTypes)) {
                List<TbKocTaskType> mustKocTaskTypeList = tbKocTaskTypeMapper.selectList(Wrappers.lambdaQuery(TbKocTaskType.class).in(TbKocTaskType::getId, mustKocTaskTypes));
                tbKocLevel.setMustKocTaskTypes(mustKocTaskTypeList);
            }
        }
        // 非必做任务类型
        String requestKocTaskType = tbKocLevel.getRequestKocTaskType();
        if (StringUtils.isNotEmpty(requestKocTaskType)) {
            List<String> requestKocTaskTypes = Arrays.stream(requestKocTaskType.split(","))
                .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(requestKocTaskTypes)) {
                List<TbKocTaskType> requestKocTaskTypeList = tbKocTaskTypeMapper.selectList(Wrappers.lambdaQuery(TbKocTaskType.class).in(TbKocTaskType::getId, requestKocTaskTypes));
                tbKocLevel.setRequestKocTaskTypes(requestKocTaskTypeList);
            }
        }
        // 升级必做
        String upRequestKocTaskType = tbKocLevel.getUpRequestKocTaskType();
        if (StringUtils.isNotEmpty(upRequestKocTaskType)) {
            List<String> upRequestKocTaskTypes = Arrays.stream(upRequestKocTaskType.split(","))
                .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(upRequestKocTaskTypes)) {
                List<TbKocTaskType> tbKocTaskTypesList = tbKocTaskTypeMapper.selectList(Wrappers.lambdaQuery(TbKocTaskType.class).in(TbKocTaskType::getId, upRequestKocTaskTypes));
                tbKocLevel.setUpRequestKocTaskTypes(tbKocTaskTypesList);
            }
        }

        // 未作降级任务类型集合id
        String downKocTaskType = tbKocLevel.getDownKocTaskType();
        if (StringUtils.isNotEmpty(downKocTaskType)) {
            List<String> downKocTaskTypes = Arrays.stream(downKocTaskType.split(","))
                .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(downKocTaskTypes)) {
                List<TbKocTaskType> tbKocTaskTypesList = tbKocTaskTypeMapper.selectList(Wrappers.lambdaQuery(TbKocTaskType.class).in(TbKocTaskType::getId, downKocTaskTypes));
                tbKocLevel.setDownKocTaskTypeTypes(tbKocTaskTypesList);
            }
        }
        return tbKocLevel;
    }
}
